package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.DTO.HospitalProfileDTO;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.HospitalProfile;
import team.yidong.fenda.entity.HospitalProfileImg;
import team.yidong.fenda.repository.HospitalProfileImgRepository;
import team.yidong.fenda.repository.HospitalProfileRepository;
import team.yidong.fenda.repository.HospitalRepository;
import team.yidong.fenda.service.HospitalProfileService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuanlong on 2018/3/15.
 */
@Service
public class HospitalProfileServiceImpl implements HospitalProfileService {
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    HospitalProfileImgRepository hospitalProfileImgRepository;
    @Autowired
    HospitalProfileRepository hospitalProfileRepository;

    @Override
    public JSONArray findHospitalProfile(Integer hospitalId) {
        List<HospitalProfile> hospitalProfileList = hospitalProfileRepository.findByHospitalId(hospitalId);
        List<HospitalProfileDTO> hospitalProfileDTOList = new ArrayList<>();
        for (HospitalProfile hospitalProfile : hospitalProfileList) {
            HospitalProfileDTO hospitalProfileDTO = new HospitalProfileDTO();
            hospitalProfileDTO.setContent(hospitalProfile.getContent());
            List<String> hospitalProfileImgList = hospitalProfileImgRepository.findHospitalProfileImg(hospitalProfile.getId());
            hospitalProfileDTO.setImgPathList(hospitalProfileImgList);
            hospitalProfileDTO.setHeadline(hospitalProfile.getHeadline());
            hospitalProfileDTOList.add(hospitalProfileDTO);
        }
        return JSONArray.fromObject(hospitalProfileDTOList);
    }

    @Transactional
    @Override
    public void addHospitalProfile(Integer hospitalId, String content, String headline, List<String> imgPathList) {
        HospitalProfile hospitalProfile = new HospitalProfile();
        hospitalProfile.setContent(content);
        hospitalProfile.setHeadline(headline);
        hospitalProfile.setHospitalId(hospitalId);
        hospitalProfile.setStatus("use");
        hospitalProfileRepository.save(hospitalProfile);
        for (String imgPath : imgPathList) {
            HospitalProfileImg hospitalProfileImg = new HospitalProfileImg();
            hospitalProfileImg.setHospitalProfileId(hospitalProfile.getId());
            hospitalProfileImg.setImgPath(imgPath);
            hospitalProfileImg.setStatus("use");
            hospitalProfileImgRepository.save(hospitalProfileImg);
        }
    }

    @Transactional
    @Override
    public void deleteHospitalProfile(Integer hospitalId) {
        List<HospitalProfile> hospitalProfileList = hospitalProfileRepository.findByHospitalId(hospitalId);
        for (HospitalProfile hospitalProfile : hospitalProfileList) {
            hospitalProfile.setStatus("overdue");
            hospitalProfileRepository.save(hospitalProfile);
            List<HospitalProfileImg> hospitalProfileImgList = hospitalProfileImgRepository.findHospitalProfileImgByHospitalProfileId(hospitalProfile.getId());
            for (HospitalProfileImg hospitalProfileImg : hospitalProfileImgList) {
                hospitalProfileImg.setStatus("overdue");
                hospitalProfileImgRepository.save(hospitalProfileImg);
            }
        }
    }

    @Transactional
    @Override
    public void updateHospitalProfile(Integer hospitalId, String content, String headline, List<String> imgPathList) {
        List<HospitalProfile> hospitalProfileList = hospitalProfileRepository.findByHospitalId(hospitalId);
        for (HospitalProfile hospitalProfile : hospitalProfileList) {
            hospitalProfile.setStatus("overdue");
            hospitalProfileRepository.save(hospitalProfile);
            List<HospitalProfileImg> hospitalProfileImgList = hospitalProfileImgRepository.findHospitalProfileImgByHospitalProfileId(hospitalProfile.getId());
            for (HospitalProfileImg hospitalProfileImg : hospitalProfileImgList) {
                hospitalProfileImg.setStatus("overdue");
                hospitalProfileImgRepository.save(hospitalProfileImg);
            }
        }
        HospitalProfile hospitalProfile = new HospitalProfile();
        if (content != null) {
            hospitalProfile.setContent(content);
        }
        if (headline != null) {
            hospitalProfile.setHeadline(headline);
        }
        hospitalProfile.setHospitalId(hospitalId);
        hospitalProfile.setStatus("use");
        hospitalProfileRepository.save(hospitalProfile);
        for (String imgPath : imgPathList) {
            HospitalProfileImg hospitalProfileImg = new HospitalProfileImg();
            hospitalProfileImg.setHospitalProfileId(hospitalProfile.getId());
            hospitalProfileImg.setImgPath(imgPath);
            hospitalProfileImg.setStatus("use");
            hospitalProfileImgRepository.save(hospitalProfileImg);
        }
    }
}
